package com.blog.util.tree;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


public class TreeUtil {


    public static List<XmTreeVo> setXmTreeSelected(List<XmTreeVo> list,List<Long> menuIds){
        for (Long menuId : menuIds) {
            for (XmTreeVo xmTreeVo : list) {
                if (xmTreeVo.getValue().equals(menuId)) {
                    xmTreeVo.setSelected(true);
                }
            }
        }
        return list;
    }


    /**
     * list转Xm-Tree数据
     * @param
     * @return
     */
    public static   List<XmTreeVo> getXmTree(List<XmTreeVo> list){
        return list.stream().filter(m -> m.getParentId() == 0).map(
                (m) -> {
                    m.setChildren(getChildrenList(m, list));
                    return m;
                }
        ).collect(Collectors.toList());
    }

    private static List<XmTreeVo> getChildrenList(XmTreeVo root, List<XmTreeVo> all) {
        return all.stream().filter(m -> {
            return Objects.equals(m.getParentId(), root.getValue());
        }).map(
                (m) -> {
                    m.setChildren(getChildrenList(m, all));
                    return m;
                }
        ).collect(Collectors.toList());
    }

    /**
     * list转LayTree数据
     * @param
     * @return
     */
    public static List<LayTreeVo> getLayTree(List<LayTreeVo> list){
        return list.stream().filter(m -> m.getParentId() == 0).map(
                (m) -> {
                    m.setChildren(getLayTreeChildrenList(m, list));
                    return m;
                }
        ).collect(Collectors.toList());
    }

    private static List<LayTreeVo> getLayTreeChildrenList(LayTreeVo root, List<LayTreeVo> all) {
        return all.stream().filter(m -> {
            return Objects.equals(m.getParentId(), root.getId());
        }).map(
                (m) -> {
                    m.setChildren(getLayTreeChildrenList(m, all));
                    return m;
                }
        ).collect(Collectors.toList());
    }

}
